package com.evil.common.application.dto.permission.app;

import cn.hutool.core.collection.CollectionUtil;
import com.evil.common.application.enums.ApplicationSourceTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AppPermissionsReqDTO {

    /**
     * 源类型
     */
    @NotNull(message = "源类型不能为空")
    private Integer sourceType;

    /**
     * 源ids
     */
    private Collection<Long> sourceIds;

    public AppPermissionsReqDTO(@NotNull(message = "sourceType not null") ApplicationSourceTypeEnum applicationSourceTypeEnum, Collection<Long> sourceIds) {
        this.sourceType = applicationSourceTypeEnum.getId();
        this.sourceIds = sourceIds;
    }

    public List<AppPermissionSourceDTO> toSources() {
        if (Objects.nonNull(sourceType) && CollectionUtil.isNotEmpty(sourceIds)) {
            return sourceIds.stream().map(e -> new AppPermissionSourceDTO(sourceType, e)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
